<?php

namespace App\Jobs;

use QL\QueryList;
use Illuminate\Support\Facades\DB;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class DigSiteJobs implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $article;
    protected $col;

    public function __construct($article,$col)
    {
        //
        $this->article  = $article;
        $this->col      = $col;
    }

    public function handle()
    {
        $data    = $this->check($this->article['link']);
        if($data){
            $index  = $this->ithome($data);
            if($index){
                $site   = DB::table('sites')->where('url', $index['url'])->first();
                if($site){
                    return false;
                }else{
                    $id = DB::table('sites')->insertGetId([
                        'column_id'     => $this->col,
                        'name'          => $index['name'],
                        'url'           => $index['url'],
                        'area_id'       => 1,
                        'keywords'      => $index['keywords'],
                        'description'   => $index['description'],
                        'icon'          => 'files/55aced970a086c4e8a0578dd8012db0a.png',
                        'content'       => json_encode([
                            'ip'    => '127.0.0.1',
                            'alexa' => 0,
                            'br'    => 0,
                            'mbr'   => 0,
                            'pr'    => 0,
                            'sbr'   => 0,
                        ]),
                        'state'         => 1,
                        'created_at'    => date('Y-m-d H:i:s',time()),
                        'updated_at'    => date('Y-m-d H:i:s',time()),
                    ]);
                    if($id){
                        $this->normalizeTag(explode(',', $index['keywords']));
                        $this->ziyuan($id);
                        return true;
                    }else{
                        return false;
                    }
                }
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    public function ithome($data){
        $ql = QueryList::html($data)->rules([
            //采集规则
            'name'          => ['.h2Title', 'text'],
            'url'           => ['.plink a', 'text'],
            'keywords'      => ['meta[name=keywords]', 'content'],
            'description'   => ['meta[name=description]', 'content'],
        ])->query()->getData(function ($item) {
            $item['keywords'] = str_replace('/', ',', $item['keywords']);
            $item['keywords'] = str_replace('|', ',', $item['keywords']);
            $item['keywords'] = str_replace('|', ',', $item['keywords']);
            $item['keywords'] = str_replace('，', ',', $item['keywords']);
            $item['keywords'] = str_replace(',,', ',', $item['keywords']);
            $item['keywords'] = str_replace('、', ',', $item['keywords']);
            $item['keywords'] = str_replace(' ', ',', $item['keywords']);
            return $item;
        });
        $data   = $ql->all();
        if($data){
            return $data;
        }else{
            return false;
        }
    }

    public function normalizeTag(array $tags)
    {
        return collect($tags)->map(function ($tag) {
            $newTag   = DB::table('tags')->where('name', $tag)->first();
            if($newTag) {
                return $newTag->id;
            }else {
                $newTopic = DB::table('tags')->insert(['name' => $tag]);
                return $newTopic->id;
            }
        })->toArray();
    }
    //抓取网址
    function check($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);//抓取网址
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Baiduspider/3.0; +http://www.baidu.com/search/spider.html)");//伪造百度蜘蛛头部
        $ip = '220.181.7.121';
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:' . $ip, 'CLIENT-IP:' . $ip));//伪造百度蜘蛛IP
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//
//        curl_setopt($ch, CURLOPT_HEADER, true); //抓取服务器信息
        curl_setopt($ch, CURLOPT_NOBODY, false); //抓取网页信息
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //支持301跳转
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 30); //网页等待时间
        $data = curl_exec($ch);
        $code = curl_getinfo($ch);
        curl_close($ch);
        if($code['http_code'] == 200){
            return $data;
//            $parse_url  = parse_url($code['url']);
//            return      $parse_url['scheme'].'://'.$parse_url['host'];
        }else{
            return false;
        }
    }

    function ziyuan($id)
    {
        $urls = array(
            env('APP_URL').'/siteinfo-'.$id.'.html',
        );
        $api = env('BAIDUZHANZHANG');
        $ch = curl_init();
        $options =  array(
            CURLOPT_URL => $api,
            CURLOPT_POST => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POSTFIELDS => implode("\n", $urls),
            CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
        );
        curl_setopt_array($ch, $options);
        $result = curl_exec($ch);
        return $result;
    }
}
